package cn.dalgen.mybatis.gen.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.dalgen.mybatis.gen.utils.db.RsHandler;

public class SqlExecutor {
    public static void query(String sql, RsHandler rsHandler) throws SQLException {
        Connection connection = DataSourceUtils.getConnection();
        boolean autoCommit = connection.getAutoCommit();
        Statement statement=null;
        try {
            connection.setAutoCommit(false);
             statement = connection.createStatement();
            statement.setFetchSize(1024);
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                rsHandler.accept(resultSet);
            }
        }finally {
            connection.setAutoCommit(autoCommit);
            if(statement!=null){
                statement.close();
            }
        }
    }
}
